package cn.alex.springaidemo.openAi;

import lombok.RequiredArgsConstructor;
import org.springframework.ai.image.ImageModel;
import org.springframework.ai.image.ImagePrompt;
import org.springframework.ai.image.ImageResponse;
import org.springframework.ai.openai.OpenAiImageOptions;
import org.springframework.ai.openai.api.OpenAiImageApi;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

/**
 * 图像模型
 *
 * @author Alex
 * @date 2024/9/20 01:03
 */
@RestController
@RequestMapping("/image/model/openAi")
@RequiredArgsConstructor
public class ImageModelController {
    private final ImageModel imageModel;

    @GetMapping("")
    public String openAi(@RequestParam(value = "msg", defaultValue = "生成一只小猫") String msg) {
        ImageResponse hd = imageModel.call(new ImagePrompt(
                msg,
                OpenAiImageOptions.builder()
                        // 生成图片数量
                        .withN(1)
                        .withModel(OpenAiImageApi.DEFAULT_IMAGE_MODEL)
                        // 图片宽度
                        .withWidth(1024)
                        // 图片高度
                        .withHeight(1024)
                        // 图片质量
                        //.withQuality("hd")
                        .build()
        ));
        return hd.getResult().getOutput().getUrl();
    }
}
